package com.yixin.vuehr.service;


import cn.hutool.core.util.ArrayUtil;
import com.yixin.vuehr.mapper.MenuMapper;
import com.yixin.vuehr.mapper.MenuRoleMapper;
import com.yixin.vuehr.model.Hr;
import com.yixin.vuehr.model.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MenuService {
    @Autowired
    MenuMapper menuMapper;
    @Autowired
    MenuRoleMapper menuRoleMapper;
    public List<Menu> getMenusByHrId() {
        return menuMapper.getMenusByHrId(((Hr) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getId());
    }
//    @Cacheable
    public List<Menu> getAllMenusWithRole(){
        return menuMapper.getAllMenusWithRole();
    }

    public List<Menu> getAllMenus() {
        return menuMapper.getAllMenus();
    }

    public List<Integer> getMidByRid(Integer rid) {
        return menuMapper.getMidByRid(rid);
    }

    @Transactional
    public boolean updateMenuRole(Integer rid, Integer[] mids) {
        menuRoleMapper.deleteByRid(rid);
        if (ArrayUtil.isNotEmpty(mids)){
            Integer result = menuRoleMapper.insertMenuRoles(rid,mids);
            return result==mids.length;
        }
        return true;
    }
}
